﻿/*---------------------------------------------------------------------*/
// fileName="MailCommon.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using System.Net;
using System.Net.Mail;

namespace SureKAM.BackUp.Notify.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// 邮件工具类
    /// </summary>
    public class MailCommon
    {
         #region privte fields
        /// <summary>
        /// SMTP服务器地址 
        /// </summary>
        private string serverName;

        /// <summary>
        /// 端口号
        /// </summary>
        private int portNumber;

        /// <summary>
        /// 发件人地址 
        /// </summary>
        private string fromAddress;

        /// <summary>
        /// 收件人地址
        /// </summary>
        private string toAddress;

        /// <summary>
        /// 标题
        /// </summary>
        private string title;

        /// <summary>
        /// 发送内容
        /// </summary>
        private string content;

        /// <summary>
        /// 发件人用户名
        /// </summary>
        private string userName;

        /// <summary>
        /// 发件人密码
        /// </summary>
        private string passWrod;
        #endregion

        #region 构造函数
        /// <summary>
        /// 发送邮件构造函数
        /// </summary>
        /// <param name="serverName">SMTP服务器地址</param>
        /// <param name="portNumber">SMTP端口号</param>
        /// <param name="fromAddress">发件人地址</param>
        /// <param name="toAddress">收件人地址</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="userName">发件人用户名</param>
        /// <param name="password">发件人密码</param>
        public MailCommon(string serverName, int portNumber, string fromAddress, string toAddress, string title, string content, string userName, string password)
        {
            this.serverName = serverName;
            this.portNumber = portNumber;
            this.fromAddress = fromAddress;
            this.toAddress = toAddress;
            this.title = title;
            this.content = content;
            this.userName = userName;
            this.passWrod = password;
        }
        #endregion

        #region private method

        #endregion

        #region public method
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns></returns>
        public bool send()
        {
            try
            {
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(ServerName);
                client.UseDefaultCredentials = false;
                client.Port = PortNumber;
                ////用户名和密码
                client.Credentials = new System.Net.NetworkCredential(UserName, PassWord);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                System.Net.Mail.MailMessage message = new MailMessage(FromAddress, ToAddress, Title, Content);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                client.Send(message);
                return true;
            }//end try
            catch (Exception e)
            {
                return false;
            }//end catch
            return false;
        }
        #endregion

        #region property
        /// <summary>
        /// 获取或设置SMTP服务器
        /// </summary>
        public string ServerName
        {
            get { return this.serverName; }
            set { this.serverName = value; }
        }

        /// <summary>
        /// 获取或设置端口号
        /// </summary>
        public int PortNumber
        {
            get { return this.portNumber; }
            set { this.portNumber = value; }
        }

        /// <summary>
        /// 获取或设置发件人地址
        /// </summary>
        public string FromAddress
        {
            get { return this.fromAddress; }
            set { this.fromAddress = value; }
        }

        /// <summary>
        /// 获取或设置收件人
        /// </summary>
        public string ToAddress
        {
            get { return this.toAddress; }
            set { this.toAddress = value; }
        }

        /// <summary>
        /// 获取或设置标题
        /// </summary>
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        /// <summary>
        /// 获取或设置内容
        /// </summary>
        public string Content
        {
            get { return this.content; }
            set { this.content = value; }
        }

        /// <summary>
        /// 获取或设置发件人用户名
        /// </summary>
        public string UserName
        {
            get { return this.userName; }
            set { this.userName = value; }
        }

        /// <summary>
        /// 获取或设置发件人密码
        /// </summary>
        public string PassWord
        {
            get { return this.passWrod; }
            set { this.passWrod = value; }
        }
        #endregion
    }
}
